/**
 * 
 */
package de.wesemann.mediahandbook.le.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import de.wesemann.jdedun.entities.mediahandbook.branches.MediahandbookBranche;
import de.wesemann.jdedun.entities.mediahandbook.companies.MediahandbookCompany;
import de.wesemann.jdedun.entities.mediahandbook.people.MediahandbookPeople;
import de.wesemann.mediahandbook.le.R;

/**
 * @author lwesema
 * 
 */
public class ExpListAdapCompanyDetail extends BaseExpandableListAdapter {
	private String[] mGroups;
	private MediahandbookCompany mCompany;
	private List<MediahandbookPeople> mPeoples;
	private String[][] mChilds;
	private Context mContext;
	private List<MediahandbookBranche> mBranch;
	private String[][] mChildKeys;

	public ExpListAdapCompanyDetail(Context context, MediahandbookCompany comp, List<MediahandbookPeople> peoples,
			List<MediahandbookBranche> branch) {
		this.mCompany = comp;
		// this.mGroups = groups;
		this.mPeoples = peoples;
		this.mContext = context;
		this.mBranch = branch;
		// createGroups();

		createChilds();

	}

	private void createChilds() {
		HashMap<String, String> childs;
		ArrayList<String> groups = new ArrayList<String>();

		int i = 0;
		if (mCompany != null) {
			i = 2;
		}
		if (mPeoples != null) {
			i++;
		}
		mChilds = new String[i][];
		mChildKeys = new String[i][];
		// mGroups = new String[i];

		// contact
		if (mCompany != null) {
			childs = getCompContact();
			mChilds[0] = (String[]) childs.values().toArray(new String[0]);
			mChildKeys[0] = (String[]) childs.keySet().toArray(new String[0]);
			groups.add(mContext.getString(R.string.reachable));
			// company details
			childs = getCompDeatils();
			mChilds[1] = (String[]) childs.values().toArray(new String[0]);
			mChildKeys[1] = (String[]) childs.keySet().toArray(new String[0]);
			groups.add(mContext.getString(R.string.company_details));
		}
		// contact persons
		if (mPeoples != null) {
			childs = getPeoples();
			mChilds[2] = (String[]) childs.values().toArray(new String[0]);
			mChildKeys[2] = (String[]) childs.keySet().toArray(new String[0]);
			groups.add(mContext.getString(R.string.contact_person));
		}

		mGroups = groups.toArray(new String[0]);

	}

	private HashMap<String, String> getCompContact() {
		HashMap<String, String> childs = new HashMap<String, String>();

		if (mCompany.getPhone_primary() != null) {
			childs.put(mContext.getString(R.string.phone_primary), mCompany.getPhone_primary());
		}
		if (mCompany.getPhone_secondary() != null) {
			childs.put(mContext.getString(R.string.phone_secondary), mCompany.getPhone_secondary());
		}
		if (mCompany.getFax_primary() != null) {
			childs.put(mContext.getString(R.string.fax_primary), mCompany.getFax_primary());
		}
		if (mCompany.getFax_secondary() != null) {
			childs.put(mContext.getString(R.string.fax_secondary), mCompany.getFax_secondary());
		}
		if (mCompany.getMobile_primary() != null) {
			childs.put(mContext.getString(R.string.mobile_primary), mCompany.getMobile_primary());
		}
		if (mCompany.getMobile_secondary() != null) {
			childs.put(mContext.getString(R.string.mobile_secondary), mCompany.getMobile_secondary());
		}
		if (mCompany.getEmail_primary() != null) {
			childs.put(mContext.getString(R.string.email_primary), mCompany.getEmail_primary());
		}
		if (mCompany.getEmail_secondary() != null) {
			childs.put(mContext.getString(R.string.email_secondary), mCompany.getEmail_secondary());
		}
		if (mCompany.getUrl_primary() != null) {
			childs.put(mContext.getString(R.string.url_primary), mCompany.getUrl_primary());
		}
		if (mCompany.getUrl_secondary() != null) {
			childs.put(mContext.getString(R.string.url_secondary), mCompany.getUrl_secondary());
		}
		return childs;
	}

	private HashMap<String, String> getCompDeatils() {
		HashMap<String, String> childs = new HashMap<String, String>();

		if (mCompany.getMain_activity() != null) {
			childs.put(mContext.getString(R.string.company_main_activity), mCompany.getMain_activity());
		}
		if (mCompany.getProducts() != null) {
			childs.put(mContext.getString(R.string.company_products), mCompany.getProducts());
		}
		if (mCompany.getResources() != null) {
			childs.put(mContext.getString(R.string.company_resources), mCompany.getResources());
		}
		if (mCompany.getPast_customers() != null) {
			childs.put(mContext.getString(R.string.company_past_costumers), mCompany.getPast_customers());
		}
		if (mBranch != null) {
			StringBuilder sb = new StringBuilder();
			for (MediahandbookBranche branch : mBranch) {
				sb.append(branch.getName() + "\n");

			}
			// sb.replace(sb.length() - 2, sb.length(), "");
			childs.put(mContext.getString(R.string.company_branch), sb.toString());
		}
		return childs;
	}

	private HashMap<String, String> getPeoples() {
		HashMap<String, String> childs = new HashMap<String, String>();

		for (MediahandbookPeople people : mPeoples) {
			String occupation = new String();
			StringBuilder sb = new StringBuilder();
			if (people.getOccupation() != null) {
				occupation = people.getOccupation();
			}
			if (people.getPosition() != null) {
				sb.append(people.getPosition() + " ");
			}
			if (people.getTitle() != null) {
				sb.append(people.getTitle() + " ");
			}
			if (people.getFirst_name() != null) {
				sb.append(people.getFirst_name() + " ");
			}
			if (people.getLast_name() != null) {
				sb.append(people.getLast_name());
			}
			childs.put(occupation, sb.toString());
		}
		return childs;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return mChilds[groupPosition][childPosition];
	}

	public Object getChildKey(int groupPosition, int childPosition) {
		return mChildKeys[groupPosition][childPosition];
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return groupPosition & childPosition;
	}

	static class ChildViewHolder {
		public TextView childKey;
		public TextView childContent;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		ChildViewHolder holder;
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.child_layout, null);

			holder = new ChildViewHolder();

			holder.childContent = (TextView) convertView.findViewById(R.id.tv_child);
			holder.childKey = (TextView) convertView.findViewById(R.id.tv_child_key);
			convertView.setTag(holder);
		} else {
			holder = (ChildViewHolder) convertView.getTag();
		}
		// TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
		holder.childKey.setText(getChildKey(groupPosition, childPosition).toString());
		holder.childContent.setText(getChild(groupPosition, childPosition).toString());
		return convertView;
	}

	public String getChildContent(int groupPosition, int childPosition) {
		return mChilds[groupPosition][childPosition];
	}

	public String getChildKeyContent(int groupPosition, int childPosition) {
		return mChildKeys[groupPosition][childPosition];
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return mChilds[groupPosition].length;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return mGroups[groupPosition];
	}

	@Override
	public int getGroupCount() {
		return mGroups.length;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	static class GroupViewHolder {
		public TextView groupName;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		GroupViewHolder holder;
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.group_layout, null);
			holder = new GroupViewHolder();

			holder.groupName = (TextView) convertView.findViewById(R.id.tvGroup);

			convertView.setTag(holder);
		} else {
			holder = (GroupViewHolder) convertView.getTag();
		}
		holder.groupName.setText(getGroup(groupPosition).toString());
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
